<!doctype html>
<html>
    <head>
        <title>Load event on the track element</title>
        <script src="../../resources/testharness.js"></script>
        <script src="../../resources/testharnessreport.js"></script>
    </head>
    <body>
        <script>

            var video;

            // Setup

            setup(function(){
                video = document.createElement('video');
                document.body.appendChild(video);
            }, {timeout: 5000});

            // Tests
            testCommands(["src", "mode", "append"]);
            testCommands(["src", "append", "mode"]);
            testCommands(["mode", "append", "src"]);
            testCommands(["mode", "src", "append"]);
            testCommands(["append", "src", "mode"]);
            testCommands(["append", "mode", "src"]);

            // Utilities

            function testCommands(commands)
            {
                var t = async_test(document.title + ", " + commands.join(" / "));
                t.step(function()
                {
                    var count = 0;
                    // Create the default <track> element.
                    var track = document.createElement('track');
                    track.kind = "subtitles";
                    // Add the "load" event listener.
                    track.addEventListener("load", t.step_func(function(event)
                    {
                        // Clean up for next test.
                        video.removeChild(track);

                        // Ensure we only got the event after all commands ran.
                        if (count === commands.length)
                            t.done();
                    }));
                    // Run each command.
                    commands.forEach(function(command)
                    {
                        count++;
                        runCommand(track, command);
                    });
                });
            };

            function runCommand(track, command)
            {
                if (command === "src")
                    track.src = "data:text/vtt," + encodeURIComponent("WEBVTT\n\n00:00:00.000 --> 00:00:01.000\ntest\n");

                if (command === "mode")
                    track.track.mode = 'hidden';

                if (command === "append")
                    video.appendChild(track);
            };

        </script>
    </body>
</html>
